var util = require('../../utils/util.js')
var app = getApp()
var rootUrl = app.globalData.rootDocment
var imgPublicUrl = rootUrl + 'Public/Home/images/'

Page({

  /**
   * 页面的初始数据
   */
  data: {
      //banner
    imgUrls: [],
    // imgUrls: [
    //   'http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg',
    //   'http://img06.tooopen.com/images/20160818/tooopen_sy_175866434296.jpg',
    //   'http://img06.tooopen.com/images/20160818/tooopen_sy_175833047715.jpg'
    // ],
    indicatorDots: true,
    autoplay: true,
    circular:true,
    interval: 5000,
    duration: 1000,
	 imageArr: {
		 indexMuneOne: imgPublicUrl + 'indexMuneOne.jpg',
		 indexMuneTwo: imgPublicUrl + 'indexMuneTwo.jpg',
		 indexMuneFour: imgPublicUrl + 'indexMuneFour.jpg',
		 indexMuneThree: imgPublicUrl + 'indexMuneThree.jpg?1'
	 },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    if (options.q){
      var strq = decodeURIComponent(options.q)
      app.globalData.scodeval = strq  //更新公共扫码参数
    }else{
      app.globalData.scodeval = null
    }
    //请求banner图片
    util.AJAX('Home/Index/getBanner', '', function (res) { //方法体
      if (res.data.StatusCode == 100) { //正常用户
        that.setData({
          imgUrls:res.data.Info
        })
      } else {
        console.log('获取Banner失败')
      }
    }, 'POST')
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
	  app.publicchangenavigate()

    var userinfostor = wx.getStorageSync('userloginfo')
    if (userinfostor) {
      //先判断用户是否正常
      util.AJAX('Home/Login/checkuser', { userinfoid: userinfostor.userInfoId }, function (res) { //方法体
        if (res.data.StatusCode == 100) { //正常用户
          app.globalData.userInfo = userinfostor.userInfo
          app.globalData.nickName = userinfostor.nickName
          app.globalData.userID = userinfostor.userID
          app.globalData.userPhone = userinfostor.userPhone
          app.globalData.userLogType = userinfostor.userLogType
          app.globalData.userInfoId = userinfostor.userInfoId
          app.globalData.loginButton = userinfostor.loginButton
          app.globalData.personalHeaderName = userinfostor.personalHeaderName
          app.globalData.switchtype = userinfostor.switchtype
        } else { //更新失败时，主动退出登录
          app.logout()
        }
      }, 'POST')
    }

    //判断登录超时
    if (app.globalData.userLogType) {
      util.AJAX('Home/Login/cklogintimeout', { userinfoid: app.globalData.userInfoId }, function (ress) { //方法体
        //执行初始值操作
        if (ress.data.StatusCode == 100) { //登录未超时
          //更新登录时间
          util.AJAX('Home/Login/uplogintime', { userinfoid: app.globalData.userInfoId }, function (res) { //方法体
            if (res.data.StatusCode == 100) {
              //无操作
            } else { //更新失败时，主动退出登录
              app.logout()
            }
          }, 'POST')
        } else { //登录超时，主动退出登录
          app.logout()
        }
      }, 'POST')
    }
  },
  onShow: function () {
    wx.getNetworkType({
      success: function (res) {
        // 返回网络类型, 有效值：
        // wifi/2g/3g/4g/unknown(Android下不常见的网络类型)/none(无网络)
        var networkType = res.networkType
        if (networkType == '2g'){
          wx.showModal({
            title: '提示',
            showCancel:false,
            content: '网络速度不给力哦',
            success: function (res) {}
          })
        } else if (networkType == 'unknown'){
          wx.showModal({
            title: '提示',
            showCancel: false,
            content: '不常见的网络类型',
            success: function (res) {}
          })
        } else if (networkType == 'none'){
          wx.showModal({
            title: '提示',
            showCancel: false,
            content: '无可用网络',
            success: function (res) {}
          })
        }
      }
    })
    //获取更新下单单价
    util.AJAX('Home/Index/getunitprice', { request: 1 }, function (res) { //方法体
      if (res.data.StatusCode == 100) { //获取成功
        app.globalData.unitprice = res.data.Info
      }
    }, 'POST')
	  /**正式版删除 */
	  // wx.setStorageSync('loginButton', false)
	  // wx.setStorageSync('personalHeaderName', true)
	  /**正式版删除 */
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
	  return app.globalshare();
  },
  //存件入口
  RichScan:function(){
    var switchtype = app.globalData.switchtype
    if (switchtype) {  //	允许跳转
      //判断是否登陆
      if (app.checkLogin()) {
        //判断有否有公共的参数，是否是智能柜参数
        var scodevals = app.globalData.scodeval  //参数
        var parmflag = false  //是否要直接跳转的状态
        if (scodevals) { //如果存在
          //判断是是否是智能柜的参数
          if (scodevals.indexOf(rootUrl) >= 0) {  //判断是否规范
            var scoderes = scodevals.replace(rootUrl, '')
            var strlenth = scoderes.replace(/[^0-9]/ig, "").length
            if (strlenth == 10) { //如果结果是智能柜参数，直接跳转传值
              parmflag = true
            }
          }
        }

        if (parmflag) {
          app.publicnavigateTologing('/pages/SavePost/SavePost?result=' + scodevals)
        } else {
          wx.scanCode({
            onlyFromCamera: true,
            success: (res) => {
              if (res.errMsg = "scanCode:ok") {
                app.publicnavigateTologing('/pages/SavePost/SavePost?result=' + res.result)
              }
            }
          })
        }
      } else {
        app.navigateTologing() //跳转到登录
      }
    } else {
      app.userlogin()
    }
  },
  personalCenterTap:function(e){
	  var switchtype = app.globalData.switchtype
	  if (switchtype){  //	允许跳转
      if (app.checkLogin()) {
        app.publicnavigateTologing('/pages/personalCenter/personalCenter')
      } else {
        app.navigateTologing() //跳转到登录
      }
	  }else{
		  app.userlogin()
	  }
  },
  //大件取货
  bigsaveTap: function(e) {
    var switchtype = app.globalData.switchtype
    if (switchtype) {  //	允许跳转
      if (app.checkLogin()) {
        app.publicnavigateTologing('/pages/BigSavePost/BigSavePost')
      } else {
        app.navigateTologing() //跳转到登录
      }
    } else {
      app.userlogin()
    }
  },
  aboutTap: function (e) {
	  app.publicnavigateTologing('/pages/brief/brief?aid=' + e.currentTarget.dataset.aid)
  },
  seekCabinetTap:function(){
	  app.publicnavigateTologing('/pages/seekCabinet/seekCabinet')
  }
})